package ldkxingzhe.ldkxingzhedemo.alarm;

import android.app.AlarmManager;
import android.app.PendingIntent;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.os.SystemClock;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.FrameLayout;
import android.widget.TextView;

import ldkxingzhe.ldkxingzhedemo.utils.LogDemo;

/**
 * Created by ke on 15-9-19.
 */
public class AlarmSleepActivity extends AppCompatActivity implements View.OnClickListener {

    private final String TAG = AlarmSleepActivity.class.getSimpleName();
    private Handler handler;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        FrameLayout layout = new FrameLayout(this);
        TextView textView = new TextView(this);
        textView.setText("Test Alarm Sleep Activity");
        layout.addView(textView);
        setContentView(layout);
        layout.setOnClickListener(this);

        handler = new Handler();
    }

    @Override
    protected void onResume() {
        super.onResume();
//        LogDemo.v(TAG,"onResume, and handler.postDelayed");
//        handler.postDelayed(runnable, 2000);
    }

    Runnable runnable = new Runnable() {
        @Override
        public void run() {
            LogDemo.v(TAG,"run execute");
            handler.postDelayed(runnable,2000);
        }
    };


    @Override
    protected void onPause() {
        super.onPause();
        LogDemo.v(TAG,"onPause, and handler.removeCallbacks");
        handler.removeCallbacks(runnable);
    }

    @Override
    public void onClick(View v) {
        LogDemo.v(TAG,"onClick");
        AlarmManager mrg = (AlarmManager) getSystemService(ALARM_SERVICE);
        Intent intent = new Intent(this,LogService.class);
        PendingIntent pendingIntent = PendingIntent.getService(this,0,intent,0);
        mrg.setRepeating(AlarmManager.ELAPSED_REALTIME_WAKEUP,
                SystemClock.elapsedRealtime() + 3000,
                3000,
                pendingIntent);
    }
}
